package com.blb.mapper.impl;

import com.blb.mapper.EmployeeDAO;
import com.blb.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EmployeeDAOImpl implements EmployeeDAO {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Employee> selectAll() {
        List<Employee> employees = jdbcTemplate.query("select * from t_employee",
                new BeanPropertyRowMapper<>(Employee.class));
        return employees;
    }

    @Override
    public List<Employee> selectEmployeesLikeName(String name) {
        List<Employee> employees = jdbcTemplate.query("select * from t_employee where name like concat('%',?,'%')",
                new BeanPropertyRowMapper<>(Employee.class),
                name);
        return employees;
    }

    @Override
    public Employee selectById(Long id) {
        Employee employee = jdbcTemplate.queryForObject("select * from t_employee where id = ?", new BeanPropertyRowMapper<>(Employee.class),
                id);
        return employee;
    }

    @Override
    public void updateEmployee(Employee employee) {
        jdbcTemplate.update("update t_employee set name=?,gender=?,age=?,dept_id=?,image=?,position=? where id =?",
                employee.getName(),employee.getGender(),employee.getAge(),employee.getDeptId(),employee.getImage(),employee.getPosition(),employee.getId());
    }
}
